using System;
using System.Collections.Generic;
using System.Text;

namespace TAAlert.CommonUtils
{
    class DateTimeUtil
    {
        /// <summary> Return index of a key-date. If the date does not exist in the list return the first date before key-date </summary>
        /// <param name="dates">List of dates</param>
        /// <param name="key">key-date to be looked up in the list of dates</param>
        /// <returns>index of the key-date or index of the first date before key-date</returns>
        static public int ixOfLessOrEqual(IList<DateTime> dates, DateTime key)
        {
            int ix = 0;
            while (dates[ix] < key && ix < (dates.Count - 1))
                ++ix;
            if (ix > 0 && dates[ix] >= key) // get the first date BEFORE startDate
                --ix;

            return ix;
        }
    }
}
